package com.controller;

import com.service.GradeService;
import com.POJO.Grade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Package com.controller
 * @Date 2024-05-23 12:09
 * @Description 成绩管理的控制器类，处理前端关于教师和学生成绩的请求
 */
@Controller
@RequestMapping("/grade")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    /**
     * 根据科目ID获取学生成绩
     * @param subjectId 科目ID
     * @return 包含学生成绩的视图
     */
    @GetMapping("/subject/{subjectId}")
    public ModelAndView getGradesBySubjectId(@PathVariable("subjectId") int subjectId) {
        List<Grade> grades = gradeService.getGradesBySubjectId(subjectId);
        return new ModelAndView("teacherGrades", "grades", grades);
    }

    /**
     * 修改学生成绩
     * @param grade 成绩对象
     * @return 重定向到学生成绩视图
     */
    @PostMapping("/edit")
    public ModelAndView editGrade(@ModelAttribute("grade") Grade grade) {
        gradeService.editGrade(grade);
        return new ModelAndView("redirect:/grade/student/" + grade.getStudentId());
    }

    /**
     * 获取学生的所有成绩
     * @param studentId 学生ID
     * @return 包含学生成绩的视图
     */
    @GetMapping("/student/{studentId}")
    public ModelAndView getGradesByStudentId(@PathVariable("studentId") int studentId) {
        List<Grade> grades = gradeService.getGradesByStudentId(studentId);
        return new ModelAndView("studentGrades", "grades", grades);
    }

    /**
     * 跳转到studentGrades页面
     * @return studentGrades视图
     */
    @GetMapping("/")
    public String toStudentGrades(){
        return "studentGrades";
    }
}
